const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

// 定义职位模型
const Job = sequelize.define('Job', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '职位标题不能为空' },
      len: { args: [3, 100], msg: '职位标题长度必须在3-100个字符之间' }
    }
  },
  industry: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '行业不能为空' }
    }
  },
  location: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '工作地点不能为空' }
    }
  },
  salaryRange: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '薪资范围不能为空' }
    }
  },
  experience: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '工作经验要求不能为空' }
    }
  },
  education: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: { msg: '学历要求不能为空' }
    }
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: false,
    validate: {
      notEmpty: { msg: '职位描述不能为空' }
    }
  },
  requirements: {
    type: DataTypes.TEXT,
    allowNull: false,
    validate: {
      notEmpty: { msg: '职位要求不能为空' }
    }
  },
  benefits: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive', 'expired'),
    defaultValue: 'active',
    allowNull: false
  },
  
}, {
  tableName: 'jobs',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['company_id'] },
    { fields: ['status'] },
    { fields: ['location'] }
  ]
});

// 关联关系
Job.associate = (models) => {
  // 职位属于公司用户
  Job.belongsTo(models.User,
    { foreignKey: 'companyId', as: 'company' });
};

module.exports = Job;